package com.fjwt.gz.mgr.ctrl.score.exchange;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.core.model.security.GzUserDetails;
import com.fjwt.gz.db.dto.ScoreExchangeDTO;
import com.fjwt.gz.db.vo.ScoreExchangeFansVO;
import com.fjwt.gz.db.vo.ScoreExchangeRecordVO;
import com.fjwt.gz.db.vo.ScoreExchangeVO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.ScoreExchangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 积分商城接口类
 */
@Slf4j
@RestController
@RequestMapping("/api/scoreExchange")
public class ScoreExchangeController extends CommonCtrl {

    @Autowired
    private ScoreExchangeService exchangeService;

    /**
     * 获取积分商城列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_MALL_INFO', 'ENT_SCORE_EQUITY_MALL_INFO')")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ApiRes getScoreExchange() {
        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
        log.info("proxy端积分商城列表请求参数：scoreExchangeDTO:{}", scoreExchangeDTO);
        IPage<ScoreExchangeVO> list = exchangeService.queryList(getIPage(), scoreExchangeDTO);
        log.info("pc端积分商城列表返回参数，请求地址：/api/scoreExchange/getList，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

    /**
     * 积分商城新增
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_MALL_INFO_ADD', 'ENT_SCORE_EQUITY_MALL_INFO_ADD')")
    @PostMapping("/save")
    public ApiRes save() {
        GzUserDetails user = getCurrentUser();
        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
        scoreExchangeDTO.setCreatedUid(user.getSysUserId());
        log.info("pc端积分商城新增，请求地址：/api/scoreExchange/save，请求参数：{}", JSONUtil.toJsonStr(scoreExchangeDTO));
        exchangeService.saveScoreExchange(scoreExchangeDTO);
        return ApiRes.ok();
    }

    /**
     * 积分商城积分兑换明细
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_MALL_INFO_EXCHANGE_RECORDS')")
    @GetMapping("/getScoreConsumeList")
    public ApiRes getScoreConsumeList() {
        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
        log.info("proxy端获取积分兑换明细，请求地址：/api/scoreExchange/getConsumeList，请求参数：{}", JSONUtil.toJsonStr(scoreExchangeDTO));
        IPage<ScoreExchangeFansVO> list = exchangeService.getScoreConsumeList(getIPage(), scoreExchangeDTO);
        log.info("proxy端获取积分兑换明细返回参数，请求地址：/api/scoreExchange/getConsumeList，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

    /**
     * 获取积分兑换明细
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_MALL_INFO_EXCHANGE_RECORDS', 'ENT_SCORE_EQUITY_MALL_INFO_EXCHANGE_RECORDS')")
    @PostMapping("/getConsumeList")
    public ApiRes getConsumeList() {
        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
        log.info("pc端获取积分兑换明细，请求地址：/api/scoreExchange/getConsumeList，请求参数：{}", JSONUtil.toJsonStr(scoreExchangeDTO));
        IPage<ScoreExchangeFansVO> list = exchangeService.selectConsumeList(getIPage(), scoreExchangeDTO);
        log.info("pc端获取积分兑换明细返回参数，请求地址：/api/scoreExchange/getConsumeList，返回结果：{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

//    /**
//     * 积分兑换明细导出Excel
//     */
//    @GetMapping("/deriveConsumeExcel")
//    public void deriveConsumeExcel() throws Exception {
//        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
//        log.info("pc端积分兑换明细导出Excel，请求地址：/api/scoreExchange/deriveConsumeExcel，请求参数：{}", JSONUtil.toJsonStr(scoreExchangeDTO));
//        exchangeService.selectDeriveConsumeList(getIPage(true), scoreExchangeDTO, response);
//    }

    /**
     * 上下架
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_MALL_INFO_EDIT', 'ENT_SCORE_EQUITY_MALL_INFO_EDIT')")
    @PostMapping("/updateIsFlag")
    public ApiRes updateIsFlag() {
        Long scoreExchangeId = getValLong("scoreExchangeId");
        Integer isFlag = getValInteger("isFlag");
        log.info("pc端积分商城草稿、上下架，请求地址：/api/scoreExchange/updateIsFlag，请求参数：scoreExchangeId:{},isFlag:{}", scoreExchangeId, isFlag);
        Boolean flag = exchangeService.updateIsFlag(scoreExchangeId, isFlag);
        return flag ? ApiRes.ok() : ApiRes.customFail("操作失败！");
    }

    /**
     * 详情
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_MALL_INFO_EDIT', 'ENT_SCORE_EQUITY_MALL_INFO_EDIT')")
    @PostMapping("/detail")
    public ApiRes detail() {
        Long scoreExchangeId = getValLong("scoreExchangeId");
        String awardType = getValString("awardType");
        log.info("pc端积分商城详情，请求地址：/api/scoreExchange/detail，请求参数：scoreExchangeId:{}, awardType:{}", scoreExchangeId, awardType);
        ScoreExchangeVO scoreExchangeVO = exchangeService.getDetails(scoreExchangeId, awardType);
        ScoreExchangeVO data = new ScoreExchangeVO();
        BeanUtils.copyProperties(scoreExchangeVO, data);
        return ApiRes.ok(data);
    }

    /**
     * 修改
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_MALL_INFO_EDIT', 'ENT_SCORE_MALL_INFO_EDIT')")
    @PostMapping("/update")
    public ApiRes update() {
        ScoreExchangeDTO scoreExchangeDTO = getObject(ScoreExchangeDTO.class);
        log.info("pc端积分商城修改，请求地址：/api/scoreExchange/detail，请求参数：scoreExchangeDTO:{}", scoreExchangeDTO);
        exchangeService.update(scoreExchangeDTO);
        return ApiRes.ok();
    }

//    /**
//     * 商城兑换记录
//     *
//     * @return
//     */
//    @PostMapping("/getExchangeList")
//    public ApiRes getExchangeList() {
//        String awardType = getValString("awardType");
//        IPage<ScoreExchangeRecordVO> list = exchangeService.getExchangeList(getIPage(), awardType);
//        return ApiRes.page(list);
//    }

}
